﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WCell.Constants.World;
using WCell.RealmServer.Entities;
using WCell.RealmServer.Global;
using World=WCell.RealmServer.Global.World;

namespace WCell.RealmServer.GameObjects
{
	public class Portal : GameObject
	{
		public static GOTemplate DefaultTemplate;

		public static Portal Create(IWorldLocation target)
		{
			var portal = (Portal)Create(DefaultTemplate.Entry, DefaultTemplate);
			portal.Target = target;
			return portal;
		}

		IWorldLocation m_Target;

		public bool IsWalkInPortal = true;

		public Portal()
		{
		}

		protected Portal(IWorldLocation target)
		{
			Target = target;
		}

		/// <summary>
		/// Can be used to set the <see cref="Target"/>
		/// </summary>
		public ZoneId TargetZoneId
		{
			get
			{
				if (Target is IWorldZoneLocation)
				{
					if (((IWorldZoneLocation)Target).ZoneInfo != null)
					{
						return ((IWorldZoneLocation)Target).ZoneInfo.Id;
					}
				}
				return ZoneId.None;
			}
			set
			{
				Target = World.GetSite(value);
			}
		}

		/// <summary>
		/// Can be used to set the <see cref="Target"/>
		/// </summary>
		public ZoneInfo TargetZone
		{
			get
			{
				if (Target is IWorldZoneLocation)
				{
					return ((IWorldZoneLocation)Target).ZoneInfo;
				}
				return null;
			}
			set
			{
				Target = value.Site;
			}
		}

		/// <summary>
		/// The target to which everyone should be teleported.
		/// </summary>
		public IWorldLocation Target
		{
			get { return m_Target; }
			set
			{
				m_Target = value;
				if (m_Target == null)
				{
					throw new Exception("Target for GOPortalEntry must not be null.");
				}
			}
		}
	}
}